

<template>
  <form action="/">
    <van-search
        v-model="searchText"
        autofocus
        show-action
        placeholder="请输入要搜索的标签"
        @search="onSearch"
        @cancel="onCancel"
    />
  </form>

  <van-divider content-position="left">已选标签</van-divider>
  <div v-if="activeIds.length === 0">请选择标签</div>

  <van-row gutter="16" style="padding:16px">
    <van-col  v-for="tag in activeIds">
    <van-tag closeable size="small" type="primary" @close="doClose(tag)">
      {{tag}}
    </van-tag>
    </van-col>
  </van-row>
  <van-divider content-position="left">选择标签</van-divider>
  <van-tree-select
      v-model:active-id="activeIds"
      v-model:main-active-index="activeIndex"
      :items="tagList"
  />
  <div style="padding: 12px;">
    <van-button block type="primary"  @click="doSearchResult">搜索</van-button>
  </div>

</template>

<script setup lang="ts">
import { ref } from 'vue';
import {useRouter} from "vue-router";
const router = useRouter()

const searchText = ref('');
//标签列表
const originTagList = [
  {
    text: '后端',
    children: [
      {text: 'Java', id: 'Java'},
      {text: 'python', id: 'python'},
      {text: 'c++', id: 'c++'},
      {text: 'c#', id: 'c#'},
      {text: 'PHP', id: 'PHP'},
      {text: 'MYSQL', id: 'MYSQL'},
      {text: 'Redis', id: 'Redis'},
    ],
  },
  {
    text: '前端',
    children: [
      {text: 'HTML', id: 'HTML'},
      {text: 'CSS', id: 'CSS'},
      {text: 'JavaScript', id: 'JavaScript'},
      {text: 'Vue', id: 'Vue'},
      {text: 'React', id: 'React'},
      {text: 'Node.js', id: 'Node.js'},
      {text: 'TypeScript', id: 'TypeScript'},
    ],
  },
  {
    text: '动漫',
    children: [
      {text: '斩神', id: '斩神'},
      {text: '画江湖之不良人', id: '画江湖之不良人'},
      {text: '斗破苍穹', id: '斗破苍穹'},
      {text: '仙逆', id: '仙逆'},
      {text: '剑来', id: '剑来'},
      {text: '完美世界', id: '完美世界'},
      {text: '全职高手', id: '全职高手'},
      {text: '炼气十万年', id: '炼气十万年'},
    ],
  },
  {
    text: '体育',
    children: [
      {text: '篮球', id: '篮球'},
      {text: '足球', id: '足球'},
      {text: '羽毛球', id: '羽毛球'},
      {text: '乒乓球', id: '乒乓球'},
      {text: '跑步', id: '跑步'},
      {text: '网球', id: '网球'},
    ],
  },
  {
    text: '性别',
    children: [
      {text: '男', id: '男'},
      {text: '女', id: '女'},
    ],
  },
  {
    text: '爱好',
    children: [
      {text: '赚钱', id: '赚钱'},
      {text: '赛车', id: '赛车'},
      {text: '旅游', id: '旅游'},
      {text: '美食', id: '美食'},
      {text: '健身', id: '健身'},
    ],
  },
]

// 标签列表
let tagList = ref(originTagList);
/**
 * 搜索过滤
 * @param val
 */
const onSearch = (val) => {
  tagList.value = originTagList.map(parentTag => {
    const tempChildren = [...parentTag.children];
    const tempParentTag = {...parentTag};
    tempParentTag.children = tempChildren.filter(item => item.text.includes(searchText.value));
    return tempParentTag;
  });

}

const onCancel = () => {
  searchText.value='';
  tagList.value = originTagList;
}
//已选中的标签
const activeIds = ref([]);
const activeIndex = ref(0);



// 移除标签
const doClose = (tag:any) => {
  activeIds.value = activeIds.value.filter(item => {
    return item !== tag;
  })
}
/**
 * 执行搜索
 */
const doSearchResult = () => {
  router.push({
    path: '/user/list',
    query: {
      tags: activeIds.value
    }
  })
}

</script>

<style scoped>


</style>